package com.ahwei.controller;

import com.ahwei.pojo.Browse;
import com.ahwei.pojo.User;
import com.ahwei.service.BrowseService;
import com.ahwei.service.impl.BrowseServiceImpl;
import com.ahwei.util.anno.Controller;
import com.ahwei.util.anno.FunctionMapping;
import com.ahwei.util.anno.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author：ahwei163@qq.com
 *
 * @date: 202*-03-15 15:37:49
 * @description: 这是一个Java文件！
 */
@Controller
public class BrowseController {
    @FunctionMapping("浏览视频")
    @RequestMapping("/browseVideo")
    public String browseVideo(HttpSession session){
        long vid = (long)session.getAttribute("vid");
        int result = browseService().addBrowse(new Browse(User(session).getUid(),vid,new Date(),null));

        return result > 0 ? "ajax:true":"ajax:false";
    }

    @FunctionMapping("判断是否浏览过该视频")
    @RequestMapping("/isBrowseVideo")
    public String isBrowseVideo(HttpSession session){
        long vid = (long)session.getAttribute("vid");
        List<Browse> browseList = getBrowseList(session);
        for (Browse browse : browseList) {
            if (browse.getVid() == vid){
                return "ajax:true";
            }
        }
        return "ajax:false";
    }

    @FunctionMapping("获取浏览列表")
    @RequestMapping("/getBrowseList")
    public List<Browse> getBrowseList(HttpSession session){
        return browseService().findBrowseByUid(User(session).getUid());
    }


    private User User(HttpSession session){
        return (User)session.getAttribute("currentUser");
    }
    private BrowseService browseService(){
        return new BrowseServiceImpl();
    }
}
